package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.constant.IsDefaultConstant;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.IAddressBookService;
import com.sky.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements IAddressBookService {
    @Override
    @Transactional
    public void updateDefault(AddressBook addressBook) {
        //先找到默认地址的
        LambdaQueryWrapper<AddressBook> lqw =new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getIsDefault, IsDefaultConstant.YES);
        lqw.eq(AddressBook::getUserId, ThreadLocalUtil.getValue());
        AddressBook one = this.getOne(lqw);
        //先取消之前的默认地址
        one.setIsDefault(IsDefaultConstant.NO);
        this.updateById(one);
        //再将现在的地址，改为默认地址
        addressBook.setIsDefault(IsDefaultConstant.YES);
        this.updateById(addressBook);

    }
}
